package com.jrzn.fragment;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.ScrollView;
import android.widget.TextView;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;
import com.jrzn.adapter.Homeadapter;
import com.jrzn.base.BaseActivity;
import com.jrzn.entity.Textbigentity;
import com.jrzn.view.CautionAlertdialog;
import com.jrzn.view.ListViewInScrollView;
import com.jrzn.view.ScrollTextView;
import com.nsjr.jrznandroid.R;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.Callback;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import okhttp3.Call;
import okhttp3.Response;
/**
 * Created by haokai on 2016/5/12.
 */
public class HomeFragment  extends BaseFragment implements ViewPager.OnPageChangeListener{
    private View view;
    private ViewPager viewPager;
        private List<ImageView> imageViews;
         private ListViewInScrollView listview;
       private Homeadapter homeadapter;
       private List<String>listStrins=new ArrayList<String>();
       private RadioButton radioButton1,radioButton2,radioButton3;
       private RadioGroup radioGroup;
      private LinearLayout linearpoints;
       private TextView view1,view2,view3;
      private PullToRefreshScrollView pullScrollView;
        private ScrollTextView textmar1,textmar2;
    //滚动属性
      private static int count = 0;
      private boolean isContinue;
       private Thread myThread;
    private Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
                case 0:
                    textmar1.next();
                    textmar2.next();
                    count++;
                    textmar1.setText("这是第" + count + "次滚动");
                    textmar2.setText("这是第" + count + "次显示");
                    break;
                default:
                    break;
            }
        };
    };

    public View onCreateView(LayoutInflater inflater,  ViewGroup container,  Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_home, null);
        init();
        return view;
    }
    private void init(){
        //初始化头部的轮播图
        initTitleIcon(view,"金融指南",R.mipmap.inon_dh,R.mipmap.icon_ss,0);
        viewPager=(ViewPager)view.findViewById(R.id.adv_pager);
        listview=(ListViewInScrollView)view.findViewById(R.id.list_bottom);
        listview.setFocusable(false);
        radioButton1=(RadioButton)view.findViewById(R.id.tab_main_radiobutton1);
        radioButton2=(RadioButton)view.findViewById(R.id.tab_main_radiobutton2);
        radioButton3=(RadioButton)view.findViewById(R.id.tab_main_radiobutton3);
        radioGroup=(RadioGroup)view.findViewById(R.id.tab_main_radiogroup);
        linearpoints=(LinearLayout)view.findViewById(R.id.liear_points);
        textmar1=(ScrollTextView)view.findViewById(R.id.tv_home_scroll1);
        textmar2=(ScrollTextView)view.findViewById(R.id.tv_home_scroll2);
        textmar1.setText("初始值1");
        textmar2.setText("初始值2");
        pullScrollView=(PullToRefreshScrollView)view.findViewById(R.id.home_pullview);
        pullScrollView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ScrollView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ScrollView> refreshView) {

            }
            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ScrollView> refreshView) {

            }
        });
        view1=(TextView)view.findViewById(R.id.view_1);
        view2=(TextView)view.findViewById(R.id.view_2);
        view3=(TextView)view.findViewById(R.id.view_3);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if(radioButton1.getId()==checkedId){
                    view1.setVisibility(View.VISIBLE);
                    view2.setVisibility(View.INVISIBLE);
                    view3.setVisibility(View.INVISIBLE);
                 }else if(checkedId==radioButton2.getId()){
                    view1.setVisibility(View.INVISIBLE);
                    view2.setVisibility(View.VISIBLE);
                    view3.setVisibility(View.INVISIBLE);
                 }else if(checkedId==radioButton3.getId()){
                    view1.setVisibility(View.INVISIBLE);
                    view2.setVisibility(View.INVISIBLE);
                    view3.setVisibility(View.VISIBLE);
                }
            }
        });
        imageViews = new ArrayList<ImageView>();
        for (int i = 0; i < 3; i++) {
            ImageView image = new ImageView(getActivity());
            image.setBackgroundResource(R.mipmap.banner);
            imageViews.add(image);
            ImageView imagepoint = new ImageView(getActivity());
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                    20, 20);
            params.setMargins(5,5,5,5);
            imagepoint.setLayoutParams(params);
             imagepoint.setBackgroundResource(i == 0 ? R.drawable.rounded
                    : R.drawable.round);
            linearpoints.addView(imagepoint);
        }
        viewPager.setAdapter(new MyPagerAdapter());
        viewPager.setOnPageChangeListener(this);
        for (int i=0;i<10;i++){
            listStrins.add("6个月");
        }
        homeadapter=new Homeadapter((BaseActivity) getActivity(),listStrins);
        listview.setAdapter(homeadapter);
        getdatat();
        CautionAlertdialog cautionAlertdialog=new CautionAlertdialog(activity,"该昵称不符合规则，请重新输入。");
        cautionAlertdialog.show();
        isContinue=true;
        myThread = new Thread() {
            public void run() {
                while (isContinue) {
                    SystemClock.sleep(10000);
                    handler.sendEmptyMessage(0);
                }
            };
        };
        myThread.start();
     }

    @Override
    public void onDestroy() {
        super.onDestroy();
        isContinue = false;
        if (myThread != null && myThread.isAlive()) {
            myThread.interrupt();
        }
    }

    private void getdatat(){
           OkHttpUtils
                   .post()
                   .url("http://192.168.0.21:8090/app/content/v_list.jhtml")
                   .addParams("channelId", "14")
                   .build()
                   .execute(new MeinvCallback() {
                       @Override
                       public void onError(Call call, Exception e) {
                           e.printStackTrace();
                       }
                       @Override
                       public void onResponse(Textbigentity textbigentity) {
                           Log.e("jsonshuju",textbigentity.getData().get(0).toString());
                       }
                   });
       }

       public abstract class MeinvCallback extends Callback<Textbigentity> {
           @Override
           public Textbigentity parseNetworkResponse(Response response) throws IOException
           {
               String string = response.body().string();
               Textbigentity women = new Gson().fromJson(string, Textbigentity .class);
               return women;
           }
       }

       class MyPagerAdapter extends PagerAdapter {
           @Override
           public int getCount() {
               // TODO Auto-generated method stub
               return imageViews.size();
           }
           @Override
           public Object instantiateItem(ViewGroup container, int position) {
                 container.addView(imageViews.get(position));
               return imageViews.get(position);
           }
           @Override
           public void destroyItem(ViewGroup container, int position, Object object) {
               container.removeView(imageViews.get(position));
           }
           @Override
           public boolean isViewFromObject(View arg0, Object arg1) {
               // TODO Auto-generated method stub
               return arg0 == arg1;
           }
       }

       private void changeDot1(int index) {
           for (int i = 0; i < imageViews.size(); i++) {
               linearpoints.getChildAt(i).setBackgroundResource(R.drawable.round);
           }
           linearpoints.getChildAt(index).setBackgroundResource(R.drawable.rounded);
       }
       @Override
       public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

       }
       @Override
       public void onPageSelected(int position) {
           changeDot1(position);
       }
       @Override
       public void onPageScrollStateChanged(int state) {

       }
   }